package com.aube.interact.fighter;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.aube.interact.fighter.util.AnimatorUtil;
import com.huyn.baseframework.utils.StringUtils;

/**
 * Created by hill on 10/24.
 */
public class BadgeView extends FrameLayout {

    private BadgeBg mBg;
    private TextView mTxt;

    public BadgeView(Context context) {
        this(context, null);
    }

    public BadgeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BadgeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(context).inflate(R.layout.aube_intera_layout_badgeview, this);

        mBg = (BadgeBg) findViewById(R.id.badge_bg);
        mTxt = (TextView) findViewById(R.id.badge_txt);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = mTxt.getMeasuredHeight();
        int width = mTxt.getMeasuredWidth();

        int minWidth = height > width ? height : width;

        measureChild(getChildAt(0), MeasureSpec.makeMeasureSpec(minWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));

        ViewGroup.LayoutParams params = mTxt.getLayoutParams();
        params.width = minWidth;
//        mTxt.measure(MeasureSpec.makeMeasureSpec(minWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));

        setMeasuredDimension(MeasureSpec.makeMeasureSpec(minWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    }

    public void setText(String text) {
        if(StringUtils.isBlank(text)) {
            AnimatorUtil.hide(this);
            return;
        }
        mTxt.setText(text);
        AnimatorUtil.show(this);
    }

    public void setText(int size) {
        if(size == 0)
            AnimatorUtil.hide(this);
        else
            setText(size + "");
    }

}
